package com.atguigu.tingshu.album.receiver;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.album.api.TrackInfoApiController;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import io.micrometer.common.util.StringUtils;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AlbumReceiver {
    @Autowired
    private TrackInfoApiController trackInfoApiController;

    /**
     * 监听声音统计
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_TRACK_STAT_UPDATE)
    public void updateTrackStat(ConsumerRecord<String,String> record){
        try {
            //获取消息数据
            String value = record.value();
            //判断
            if (StringUtils.isNotEmpty(value)){
                TrackStatMqVo trackStatMqVo = JSON.parseObject(value, TrackStatMqVo.class);
                trackInfoApiController.updateTrackStat(trackStatMqVo);
                log.info("[专辑服务]，监听到更新声音统计消息：{}", value);
            }
        } catch (Exception e) {
            log.info("[专辑服务]，监听到更新声音统计消息：{}，出现异常",e.getMessage() );
            throw new GuiguException(400,"监听到更新声音统计消息出现异常"+e.getMessage());
        }

    }
}
